

local function get_common_inst()
    local inst = require("prefabs/npc_characters/__00__human_npc_common")():Common_Npc_Create()
    return inst
end

local function ghost_fx()
    local inst = CreateEntity()
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    inst:AddTag("INLIMBO")
    inst:AddTag("FX")
    -- inst:DoTaskInTime(5,inst.Remove)
    -- FX:ListenForEvent("animqueueover",FX.Remove)
    -- FX:ListenForEvent("animoever",FX.Remove)

    return inst
end

local function wilson()
    local inst = get_common_inst()
    inst.soundsname = "wilson"
    inst.prefab_base = "wilson"
    -- inst.talk_sound = "dontstarve/characters/wilson/talk_LP"
    return inst
end

local function willow()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("willow")
    inst.soundsname = "willow"
    inst.prefab_base = "willow"
    -- inst.talk_sound = "dontstarve/characters/willow/talk_LP"
    inst.customidleanim = function(inst)    -- 站立休闲动作
        if not TheWorld.ismastersim then
            return nil
        end
        local item = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
        return item ~= nil and item.prefab == "bernie_inactive" and "idle_willow" or nil
    end

    return inst
end

local function wolfgang()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("wolfgang")
    inst.soundsname = "wolfgang"
    inst.prefab_base = "wolfgang"
    inst.customidleanim = "idle_wolfgang"  -- 站立休闲动作
    -- inst.talk_sound = "dontstarve/characters/wolfgang/talk_LP"
    return inst
end

local function wendy()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("wendy")
    inst.soundsname = "wendy"
    inst.prefab_base = "wendy"
    inst.customidleanim = "idle_wendy"
    -- inst.talk_sound = "dontstarve/characters/wendy/talk_LP"
    return inst
end

local function wx78()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("wx78")
    inst.prefab_base = "wx78"
    inst.soundsname = "wx78"
    -- inst.talk_sound = "dontstarve/characters/wx78/talk_LP"
    return inst
end

local function wickerbottom()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("wickerbottom")
    inst.prefab_base = "wickerbottom"
    inst.soundsname = "wickerbottom"
    -- inst.talk_sound = "dontstarve/characters/wickerbottom/talk_LP"
    inst.customidleanim = function(inst)
        if not TheWorld.ismastersim then
            return nil
        end

        return inst.AnimState:CompareSymbolBuilds("hand", "hand_wickerbottom") and "idle_wickerbottom" or nil   
          
    end
    return inst
end

local function woodie()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("woodie")
    inst.prefab_base = "woodie"
    inst.soundsname = "woodie"
    -- inst.talk_sound = "dontstarve/characters/woodie/talk_LP"
    inst:AddTag("woodcutter")   --- 需要这个才能拿起lucy
    local function customidleanimfn(inst)
        if not TheWorld.ismastersim then
            return nil
        end
        local item = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
        return item ~= nil and item.prefab == "lucy" and "idle_woodie" or nil
    end
    inst.customidleanim = customidleanimfn

    return inst
end
local function wes()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("wes")
    inst.soundsname = "wes"
    inst.prefab_base = "wes"

    inst:AddTag("mime") -- 哑剧动作需要
    return inst
end

local function waxwell()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("waxwell")
    inst.soundsname = "maxwell"
    inst.prefab_base = "waxwell"
    -- inst.talk_sound = "dontstarve/characters/maxwell/talk_LP"

    local BOOK_MUST_TAGS = { "book", "shadowmagic" }
    local BOOK_CANT_TAGS = { "INLIMBO", "fueldepleted" }
    local function customidleanimfn(inst)
        if not TheWorld.ismastersim then
            return nil
        end
        local x, y, z = inst.Transform:GetWorldPosition()
        for i, v in ipairs(TheSim:FindEntities(x, y, z, 3, BOOK_MUST_TAGS, BOOK_CANT_TAGS)) do
            if v.isfloating then
                --secret idle anim near floating codex umbra
                --takes priority over inst.customidlestate
                return "idle3_waxwell"
            end
        end
    end
    inst.customidleanim = customidleanimfn --priority when not returning nil

    return inst
end
local function wathgrithr()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("wathgrithr")
    inst.prefab_base = "wathgrithr"
    inst.soundsname = "wathgrithr"
    -- inst.talk_sound = "dontstarve_DLC001/characters/wathgrithr/talk_LP"
    inst.customidleanim = "idle_wathgrithr"

    return inst
end
local function webber()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("webber")
    inst.prefab_base = "webber"
    inst.soundsname = "webber"
    -- inst.talk_sound = "dontstarve_DLC001/characters/webber/talk_LP"
    inst.customidleanim = "idle_webber"
    return inst
end
local function winona()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("winona")
    inst.prefab_base = "winona"
    inst.soundsname = "winona"
    -- inst.talk_sound = "dontstarve/characters/winona/talk_LP"
    inst.customidleanim = "idle_winona"
    return inst
end
local function warly()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("warly")
    inst.prefab_base = "warly"
    inst.soundsname = "warly"
    -- inst.talk_sound = "dontstarve/characters/warly/talk_LP"
    inst.customidleanim = "idle_warly"
    return inst
end
local function walter()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("walter")
    inst.prefab_base = "walter"
    inst.soundsname = "walter"
    -- inst.talk_sound = "dontstarve/characters/walter/talk_LP"
    inst.customidleanim = "idle_walter"

    if not TheWorld.ismastersim then
        return inst
    end
    local function SpawnWoby(inst)
        local player_check_distance = 40
        local attempts = 0
    
        local max_attempts = 30
        local x, y, z = inst.Transform:GetWorldPosition()
    
        local woby = SpawnPrefab("wobysmall")
        inst.woby = woby
        woby:LinkToPlayer(inst)
        inst:ListenForEvent("onremove", inst._woby_onremove, woby)
    
        while true do
            local offset = FindWalkableOffset(inst:GetPosition(), math.random() * PI, player_check_distance + 1, 10)
    
            if offset then
                local spawn_x = x + offset.x
                local spawn_z = z + offset.z
    
                if attempts >= max_attempts then
                    woby.Transform:SetPosition(spawn_x, y, spawn_z)
                    break
                elseif not IsAnyPlayerInRange(spawn_x, 0, spawn_z, player_check_distance) then
                    woby.Transform:SetPosition(spawn_x, y, spawn_z)
                    break
                else
                    attempts = attempts + 1
                end
            elseif attempts >= max_attempts then
                woby.Transform:SetPosition(x, y, z)
                break
            else
                attempts = attempts + 1
            end
        end
    
        return woby
    end
    inst:DoTaskInTime(0.5,function()
        local woby = SpawnWoby(inst)
        local x,y,z = inst.Transform:GetWorldPosition()
        woby.Transform:SetPosition(x+2,y,z)
        inst.npc_base_lib:Add2Child(woby)
        woby:AddTag("INLIMBO")
    end)

    -- inst:DoPeriodicTask(1,function()
    --     if inst.__woby then
    --         local tempDistance = inst.npc_base_lib:Distance_Targets(inst,inst.__woby)
    --         print("distance woby : ",tempDistance)
    --         if  tempDistance > 3 then
    --             inst.__woby.components.locomotor:GoToPoint(Vector3(inst.Transform:GetWorldPosition()))
    --         end
    --     end
    -- end)


    return inst
end
local function wonkey()
    local inst = get_common_inst()
    inst.AnimState:SetBuild("wonkey")
    inst.prefab_base = "wonkey"
    inst.soundsname = "wonkey"
    -- inst.talk_sound = "monkeyisland/characters/wonkey/talk_LP"
    inst.customidleanim = "idle_wonkey"
    return inst
end
return Prefab("npc_ghost_fx_for_respawn",ghost_fx),
        Prefab("wilson_npc", wilson),
        Prefab("willow_npc",willow),
        Prefab("wolfgang_npc",wolfgang),
        Prefab("wx78_npc",wx78),
        Prefab("wickerbottom_npc",wickerbottom),
        Prefab("woodie_npc",woodie),
        Prefab("wes_npc",wes),
        Prefab("waxwell_npc",waxwell),
        Prefab("wathgrithr_npc",wathgrithr),
        Prefab("webber_npc",webber),
        Prefab("winona_npc",winona),        
        Prefab("warly_npc",warly),
        Prefab("walter_npc",walter),
        Prefab("wonkey_npc",wonkey),
        Prefab("wendy_npc",wendy)